﻿using Newtonsoft.Json;
using Newtonsoft.Json.Converters;
using Newtonsoft.Json.Linq;
using System;
using System.IO;

namespace YunNanHealthProject_Register.Utils
{
    class JsonHelper
    {
        //将数据写入文件
        public static void WriteJosn(string path, object t)
        {
            //if (!File.Exists(path))
            //{
            //    File.Create(path);
            //}

            //把模型数据写到文件 
            //using (StreamWriter sw = new StreamWriter(path, true))
            using (StreamWriter sw = new StreamWriter(path))
            {
                JsonSerializer serializer = new JsonSerializer();
                serializer.Converters.Add(new JavaScriptDateTimeConverter());
                serializer.NullValueHandling = NullValueHandling.Ignore;

                //构建Json.net的写入流 
                JsonWriter writer = new JsonTextWriter(sw);
                //把模型数据序列化并写入Json.net的JsonWriter流中 
                serializer.Serialize(writer, t);
                //ser.Serialize(writer, ht); 
                writer.Close();
                sw.Close();
            }
        }

        //将文本数据读出
        public static string ReadJson(string path)
        {
            using (StreamReader file = File.OpenText(path))
            {
                return file.ReadToEnd();
            }
        }

        //随机数生成
        public static string GetRandom()
        {
            Random r = new Random(int.Parse(DateTime.Now.ToString("HHmmssfff")));
            int num = r.Next(10, 99);
            return num.ToString();
        }
    }
}
